﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Rename
{
    public partial class Form9 : Form
    {
        // 用于编辑的文本框
        private TextBox editTextBox;
        // 当前正在编辑的子项
        private ListViewItem.ListViewSubItem currentSubItem;
        public Form9()
        {
            InitializeComponent();

            InitializeListView();
            InitializeEditTextBox();
        }

        private void InitializeListView()
        {
            // 设置ListView属性
            listView1.View = View.Details;
            listView1.FullRowSelect = true;
            listView1.GridLines = true;

            // 添加列
            listView1.Columns.Add("ID", 50);
            listView1.Columns.Add("名称", 150);
            listView1.Columns.Add("值", 200);

            // 添加示例数据
            listView1.Items.Add(new ListViewItem(new[] { "1", "项目A", "100" }));
            listView1.Items.Add(new ListViewItem(new[] { "2", "项目B", "200" }));
            listView1.Items.Add(new ListViewItem(new[] { "3", "项目C", "300" }));

            // 绑定双击事件用于启动编辑
            listView1.MouseDoubleClick += ListView1_MouseDoubleClick;
        }

        private void InitializeEditTextBox()
        {
            editTextBox = new TextBox();
            editTextBox.Visible = false;
            editTextBox.KeyDown += EditTextBox_KeyDown;
            editTextBox.LostFocus += EditTextBox_LostFocus;
            listView1.Controls.Add(editTextBox);
        }

        private void ListView1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            // 获取双击位置的子项
            ListViewHitTestInfo hitTest = listView1.HitTest(e.X, e.Y);

            // 只处理子项的双击
            if (hitTest.SubItem != null)
            {
                StartEditing(hitTest.SubItem);
            }
        }

        private void StartEditing(ListViewItem.ListViewSubItem subItem)
        {
            if (subItem == null) return;

            // 记录当前正在编辑的子项
            currentSubItem = subItem;

            // 设置文本框位置和大小
            editTextBox.Bounds = subItem.Bounds;
            editTextBox.Text = subItem.Text;
            editTextBox.Visible = true;
            editTextBox.Focus();
            editTextBox.SelectAll();
        }

        private void EndEditing()
        {
            if (currentSubItem != null && editTextBox.Visible)
            {
                // 更新子项文本
                currentSubItem.Text = editTextBox.Text;
                editTextBox.Visible = false;
                currentSubItem = null;
            }
        }

        private void EditTextBox_KeyDown(object sender, KeyEventArgs e)
        {
            // 按Enter键结束编辑
            if (e.KeyCode == Keys.Enter)
            {
                EndEditing();
                e.SuppressKeyPress = true; // 防止发出提示音
            }
            // 按Esc键取消编辑
            else if (e.KeyCode == Keys.Escape)
            {
                editTextBox.Visible = false;
                currentSubItem = null;
            }
        }

        private void EditTextBox_LostFocus(object sender, EventArgs e)
        {
            // 失去焦点时结束编辑
            EndEditing();
        }
    }
}
